<template>
    <Transition name="msg">
        <div :class="styleClass" :style="{ top: state.top + 'px' }" v-show="state.show">
            {{ txt }}
        </div>
    </Transition>
</template>

<script setup lang="ts">
import { computed, reactive } from 'vue';
import { IProps, Types, IExpose, IState } from './types.ts';


defineOptions({
    name: 'Message'
});
const props = withDefaults(defineProps<IProps>(), {
    txt: 'this is default txt',
    type: Types.MSG,
});

const state: IState = reactive({
    top: 0,
    show: false
});
const styleClass = computed(() => `message-container message-${props.type}`);


function setVisible(flag: boolean): Promise<void> {
    return new Promise<void>((resolve) => {
        state.show = flag;
        setTimeout(() => {
            resolve();
        }, 300);
    });
}

function setTop(top: number) {
    state.top = top;
}


const height = 40,
    margin = 20;
defineExpose<IExpose>({
    height,
    margin,
    setTop,
    setVisible
});

</script>

<style scoped>
.message-container {
    position: fixed;
    top: 20px;
    left: 50%;
    transform: translate(-50%, -50%);
    text-align: center;
    min-height: calc(v-bind('height') * 1px);
    min-width: 150px;
    padding: 5px 20px;
    margin: calc(v-bind('margin') * 1px);
    line-height: calc(v-bind('height') * 1px);
    border-radius: 4px;
    box-sizing: border-box;
    box-shadow: 1px 2px 5px #ccc;
    font-size: 14px;
}

.msg-enter-from,
.msg-leave-to {
    transform: translateY(-20px);
    opacity: 0;
    transition: .3s;
}

.msg-enter-active,
.msg-leave-active {
    transition: .3s;
}
</style>
